package model;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class National  {
	private int nid;
	private int uid;
	private String n_name;
	private int position;
	public National(){}
	public National(int nid, int uid, String n_name, int position) {
		this.nid = nid;
		this.uid = uid;
		this.n_name = n_name;
		this.position = position;
	}
	
	public int getNid() {
		return nid;
	}
	public void setNid(int nid) {
		this.nid = nid;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getN_name() {
		return n_name;
	}
	public void setN_name(String n_name) {
		this.n_name = n_name;
	}
	public int getPosition() {
		return position;
	}
	public void setPosition(int position) {
		this.position = position;
	}
	public int getSizeDB(){
		int result = 0;
		PreparedStatement sqlu = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlu = con
					.prepareStatement("select COUNT(nid) from national");
			ResultSet rs = sqlu.executeQuery();
			while (rs.next()) {
				return rs.getInt(1);
			}
			sqlu.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public String checkName() {
		String name = this.n_name;
			if(name == null){
				return "Bạn chưa nhập National";
			}else{
				name =name.trim();
				if (name.equals("")) {
					return "Bạn chưa nhập National";
				}else{
					PreparedStatement sqlp = null;
					Connection connection = new ConnectDB().getConnection();
					try {
						sqlp = connection.prepareStatement("SELECT * FROM national WHERE n_name = ?");
						sqlp.setString(1, name);
						ResultSet rs = sqlp.executeQuery();
						while(rs.next() == true){
							return "National đã tồn tại.";
						}
					
						connection.close();
					} catch (Exception e) {
						e.printStackTrace();
					}	
					
				}
			}
		return "";
	}
	public String checkNewName() {
		String name = this.n_name;
			if(name == null){
				return "Bạn chưa nhập National";
			}else{
				name =name.trim();
				if (name.equals("")) { return "Bạn chưa nhập National";}
				else if(this.getNaByName(name).getNid()!=this.getNid()){
					return "National đã tồn tại. ";
			}
			}
		return "";
	}
	public boolean Create() {
		PreparedStatement sqlr = null;
		boolean result = false;

		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("INSERT INTO national(uid , n_name, position) VALUES (?, ?, ?)");
			sqlr.setInt(1, this.uid);
			sqlr.setString(2, this.n_name);
			sqlr.setInt(3, this.position);
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public boolean upDate() {
		PreparedStatement sqlr = null;
		boolean result = false;

		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("UPDATE national SET uid = ?,  n_name = ?, position = ? WHERE nid = ? LIMIT 1");
			sqlr.setInt(1, this.getUid());
			sqlr.setString(2, this.getN_name());
			sqlr.setInt(3, this.getPosition());
			sqlr.setInt(4, this.getNid());
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public National getNational(int nid){
		National national = null;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT * FROM national WHERE nid = ?");
			sqlp.setInt(1, nid);
			ResultSet rs = sqlp.executeQuery();
			while(rs.next() == true){
				int nid1 = rs.getInt(1);
				int uid = rs.getInt(2);
				String n_name = rs.getString(3);
				int pt = rs.getInt(4);
				national = new National(nid1, uid, n_name, pt);
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return national;
	}
	public National getNaByName(String naname) {
		naname = (naname == null) ? this.getN_name() : naname;
		PreparedStatement sqll = null;
		National result = null;
		Connection c = new ConnectDB().getConnection();
		try {
			sqll = c.prepareStatement("select * from national where n_name = ?");
			sqll.setString(1, naname);
			ResultSet rs = sqll.executeQuery();
			while (rs.next() == true) {
				int nid1 = rs.getInt(1);
				int uid = rs.getInt(2);
				String n_name = rs.getString(3);
				int pt = rs.getInt(4);
				result = new National(nid1, uid, n_name, pt);
			}
			sqll.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}
	public int[] getNID(String orby /*n_name */){
		if(orby==null){
			orby="position";
		}else{
			orby=orby.trim();
			if (!((orby.equals("nid"))||(orby.equals("n_name"))||(orby.equals("uid")) )) {
				orby="position";
			}
		}
		int[] result = new int[this.getSizeDB()];
		int i =0;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT nid FROM national ORDER BY "+ orby + " ASC  ");
			ResultSet rs = sqlp.executeQuery();
			while(rs.next() == true){
				result[i] = rs.getInt(1);
				i++;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public String toString (){
		return this.nid + " " + this.uid + " " +  this.n_name + " " +  this.position;
	}
	
	public boolean isNID(String nid) throws IOException{ /*kiem tra tinh hop le cua nid va co nam trong DB hay khong*/
		if(nid==null) return false;
		if(nid.trim().equals(""))return false;
		nid.trim();
		int id = -1;
		try {
			id = Integer.parseInt(nid);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT nid FROM national where nid= ? ");
			sqlp.setInt(1, id);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				return true;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean del(int nid){
		PreparedStatement delpd = null;
		Connection con = new ConnectDB().getConnection();
		try {
			delpd = con.prepareStatement("DELETE FROM national WHERE nid = ?");
			delpd.setInt(1, nid);
			int number = delpd.executeUpdate();
			if (number > 0) {
				return true;
			}
			delpd.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  false;
	}
}
